package com.cxrj.zhsq.workorderManage.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.cxrj.zhsq.workorderManage.dao.PropertyResourceMapper;
import com.cxrj.zhsq.workorderManage.entity.PropertyResource;
import com.cxrj.zhsq.workorderManage.service.PropertyResourceService;

@Service
public class PropertyResourceServiceImpl implements PropertyResourceService {

	@Resource
	private PropertyResourceMapper propertyResourceMapper;

	@Override
	public Map<String, Object> seletePropertyResourcePage(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		sqlMap.put("communityId", request.getSession().getAttribute("communityId"));
		List<PropertyResource> list = propertyResourceMapper.selectPropertyResourcesPage(sqlMap);
		List<Map<String, Object>> maps = new ArrayList<Map<String, Object>>();
		for (PropertyResource propertyResource : list) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", propertyResource.getId());
			if (propertyResource.getpId().equals("0")) {
				map.put("pId", 0);
			} else {
				map.put("pId", propertyResource.getpId());
			}
			map.put("createTime", propertyResource.getCreateTime());
			map.put("content", propertyResource.getContent());
			map.put("num", propertyResource.getNum());
			map.put("isFinal", propertyResource.getIsFinal());
			maps.add(map);
		}
		resultMap.put("rows", maps);
		return resultMap;
	}

	@Override
	public Map<String, Object> insertPropertyResource(PropertyResource propertyResource) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Integer c = 0;
		if (propertyResource.getId().equals("")) {
			Map<String, Object> sqlMap = new HashMap<String, Object>();
			propertyResource.setId(UUID.randomUUID().toString());
			propertyResource.setCreateTime(new Date());
			sqlMap.put("pId", propertyResource.getpId());
			sqlMap.put("communityId", propertyResource.getCommunityId());
			String num = propertyResourceMapper.selectByNum(sqlMap);
			if (num != null && !num.equals("")) {
				propertyResource.setNum((Integer.parseInt(num) + 1) + "");
			} else {
				propertyResource.setNum("1");
			}
			c = propertyResourceMapper.insertSelective(propertyResource);
		} else {
			c = propertyResourceMapper.updateByPrimaryKeySelective(propertyResource);
		}
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> deletePropertyResource(String id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if (propertyResourceMapper.selectPropertyResourceByPId(id) != null) {
			resultMap.put("result", -1);
			return resultMap;
		}
		Integer c = propertyResourceMapper.deleteByPrimaryKey(id);
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> selectPropertyResourceById(String id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("resource", propertyResourceMapper.selectByPrimaryKey(id));
		return resultMap;
	}

	@Override
	public Map<String, Object> selectPropertyResourceByPId(String pId) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("resource", propertyResourceMapper.selectByPrimaryKey(pId));
		return resultMap;
	}
}
